package com.sms.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.system.comm.util.Snowflake;
import com.system.common.mapper.BisCustomUserMapper;
import com.system.common.mapper.BisWechatRecordMapper;
import com.system.common.pojo.BisCustomUser;
import com.system.common.pojo.BisCustomUserExample;
import com.system.common.pojo.BisCustomUserExample.Criteria;
import com.system.common.pojo.BisWechatRecord;


@Service
public class BisWechatRecordServiceImpl implements BisWechatRecordService{
    @Autowired
    private BisWechatRecordMapper bisWechatRecordMapper;
    @Autowired
    private BisCustomUserMapper bisCustomUserMapper;
	@Override
	public List<Map<String,Object>> selectAll(Map<String,Object> map) {
		return this.bisWechatRecordMapper.selectAll(map);
	}
	@Override
	public void delBisWechatRecord(String recordId) {
		bisWechatRecordMapper.deleteByPrimaryKey(recordId);
		
	}
	@Override
	public List<BisCustomUser> queryBisCustomUser(Map<String,Object> map) {
		// TODO Auto-generated method stub
		String qmobile=(String)map.get("qmobile");
		String qusername=(String)map.get("qusername");
		BisCustomUserExample example=new BisCustomUserExample(); 
		Criteria criteria=example.createCriteria();
		if (StringUtils.isNotBlank(qmobile)) {
			criteria.andMobileLike("%"+qmobile+"%");
			
		}
		if (StringUtils.isNotBlank(qusername)) {
			criteria.andUsernameLike("%"+qusername+"%");
			
		}
		List<BisCustomUser> list=bisCustomUserMapper.selectByExample(example);
		return list;
	} 

	@Override
	public void saveBisWechatRecord(List<String> openid,String content,String userId) {



		boolean blean1=com.wechat.util.WechatUtil.sendTextMessage(openid,content);
		if(blean1){
			for(@SuppressWarnings("unused") String id:openid){
				 BisWechatRecord bRecord=new BisWechatRecord();
				 Snowflake snowflake=new Snowflake(0, 0);
				 long lid=snowflake																																																																																																																												.nextId();
				 String recordId=lid+"";
				 bRecord.setRecordId(recordId);
				 bRecord.setContent(content);
				 bRecord.setSendUserId(userId);
				 bRecord.setCreateUserId(recordId);
				 bRecord.setStatus("1");		 
				 bRecord.setFailureCause(null);
				 this.bisWechatRecordMapper.insert(bRecord);
			}
		}
	}
    
}

